﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Starfire3d.Interfaces;

namespace Starfire3d.Data.Objects
{
    public class ControlSheet : IDataItem, ICloneable
    {
        #region Attributes
        private ShipClass shipClass = null;
        private UnitDetails details = new UnitDetails();
        private string ctrSheet = "";
        #endregion

        #region Public Properties
        public ShipClass Class
        {
            get { return shipClass; }
            protected set { shipClass = value; }
        }

        public UnitDetails Details
        {
            get { return details; }
            protected set { details = value; }
        }
        #endregion

        #region Protected Properties
        protected string Sheet
        {
            get { return ctrSheet; }
            set { ctrSheet = value; }
        }
        #endregion

        public ControlSheet()
        {
        }

        #region Private Methods
        private bool ParseSheet()
        {
            string[] subSystems = Sheet.Split(',');

            string system = "";
            int number = 1;

            foreach (string subSys in subSystems)
            {
                string[] systems = subSys.Split('*');

                if (systems.Length == 1)
                {
                    system = systems[0].Trim();
                    number = 1;
                }
                else
                {
                    system = systems[0].Trim();
                    number = int.Parse(systems[1].Trim());
                }

                for (int i = 0; i < number; i++)
                {
                    details.AddSystem(DataItemStore.GetShipSystem(system).Clone() as ShipSystem, shipClass);
                }
            }

            return true;
        }
        #endregion

        #region IDataItem Members

        public bool BuildFromMetaData(MetaObject meta)
        {
            foreach (string token in meta.TokenList)
            {
                string[] tokens = token.Split(':');

                // Single line Tags
                if (tokens.Count() == 1)
                {
                    continue;
                }

                if (tokens[0].ToLower() == "class")
                {
                    Class = DataItemStore.GetShipClass(tokens[1]);
                    continue;
                }
                if (tokens[0].ToLower() == "sheet")
                {
                    Sheet = tokens[1];
                    continue;
                }                
            }

            return ParseSheet();
        }

        public bool IsDestroyed
        {
            get { return !Details.Systems.Any(sys => !sys.Data.GetBool("SHIELD") && !sys.IsDestroyed); }
            //get { return Details.Systems.Count(sys => sys.IsDestroyed) == Details.Systems.Count(); }
        }

        #endregion

        #region ICloneable Members

        protected ControlSheet(ControlSheet sheet)
        {
            shipClass = sheet.shipClass;
            ctrSheet = sheet.ctrSheet;

            details = sheet.details.Clone() as UnitDetails;
        }

        public object Clone()
        {
            return new ControlSheet(this);
        }

        #endregion
    }
}
